package Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class DateFolderGenerator {
    public static void main(String[] args) {

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前是" + now.format(dtf) + "，请输入创建的文件夹天数");
        Scanner sc = new Scanner(System.in);
        int day = sc.nextInt();
        File root = new File("D:\\date");
        if (!root.exists()) {
            root.mkdir();
        }
        for (int i = 1; i <= day; i++) {
            LocalDateTime targetDay = now.plusDays(i);
            File newFolder = new File(root, targetDay.format(dtf));
            if (!newFolder.exists()) {
                newFolder.mkdir();
            }
            File newFile = new File(newFolder, targetDay.format(dtf) + ".md");
            if (!newFile.exists()) {
                try (
                        FileWriter fw = new FileWriter(newFile)
                ) {
                    fw.write("这是 " + targetDay.format(dtf) + " 的文件夹。");
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}